//
//  CarouselTableView.m
//  ASHT_IOS
//
//  Created by 宋安安 on 14-4-4.
//  Copyright (c) 2014年 宋安安. All rights reserved.
//

#import "CarouselTableView.h"
#import "CarouselTableViewCell.h"

#define degressToRadians(X) ( M_PI * (X) / 180 )
@implementation CarouselTableView
@synthesize arr_new;
@synthesize ctvdelegate;
@synthesize dic_DataSource_View;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.dataSource=self;
        self.delegate=self;
        self.pagingEnabled=YES;
        [self setSeparatorColor:[UIColor clearColor]];
        self.showsVerticalScrollIndicator=NO;
        dic_DataSource_View=[NSMutableDictionary dictionary];
        
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return arr_new.count;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int i=scrollView.contentOffset.y/320;
   [self.ctvdelegate ScrollCarouselTableViewDelegate:i];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *CellIdentifier = @"Cell";
//
	CarouselTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if(cell == nil)
	{
        NSArray *nibs=[[NSBundle mainBundle] loadNibNamed:@"CarouselTableViewCell" owner:self options:nil];
        if (nibs.count>0) {
            cell =[nibs objectAtIndex:0];
        }
        cell.transform=CGAffineTransformMakeRotation(degressToRadians(90));
        [cell.imageview setupView:[UIImage imageNamed:@"l_title.png"] delegate:nil];
	}
    
    NSString *str_Key=[NSString stringWithFormat:@"%d",indexPath.row];
    
    CarouselTableViewCell *view=(CarouselTableViewCell*)[dic_DataSource_View objectForKey:str_Key];
    if (view==nil) {
        //行是否允许点击事件
        tableView.allowsSelection=YES;
        //行选中的样式
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
        cell.lab_label1.text=[[arr_new objectAtIndex:indexPath.row] objectForKey:@"title"];
        [cell imageUrl:[[arr_new objectAtIndex:indexPath.row] objectForKey:@"imageUrl"]];
        [self.ctvdelegate ScrollCarouselTableViewDelegate:indexPath.row];
        view=cell;
        [dic_DataSource_View setObject:view forKey:str_Key];
    }
    else
    {
        cell=view;
    }
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 320;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

-(void)dealloc
{
    [dic_DataSource_View removeAllObjects];
}


@end
